package top.callname.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;



import top.callname.bean.AdminBean;
import top.callname.bean.ClassBean;
import top.callname.service.ClassService;
import top.callname.service.StudentService;
import top.callname.service.impl.ClassServiceImpl;
import top.callname.service.impl.StudentServiceImpl;
import top.callname.util.BaseUtil;


@WebServlet("/admin/class")
public class ClassManagerServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(ClassManagerServlet.class);
	private ClassService classService = new ClassServiceImpl();
	private StudentService studentService = new StudentServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String method = request.getParameter("method");
		AdminBean admin = (AdminBean) request.getSession().getAttribute("adminInfo");
		List<ClassBean> list = classService.getList(admin.getPid());
		request.setAttribute("classList", list);
		logger.debug("班级信息："+list.toString());
		if("list".equals(method)){ 
			//分页查询
			String pageNo = request.getParameter("pageNo");
			String cid = request.getParameter("cid");
			int page = BaseUtil.getPageNo(pageNo);
			if(cid!=null){
				request.setAttribute("page", studentService.getListPage(page, Integer.valueOf(cid)));
				request.setAttribute("currentClassId", cid);
			}else{
				if(list!=null && list.size()>0){
					Integer cid2 = list.get(0).getCid();
					request.setAttribute("page", studentService.getListPage(page, cid2));
					request.setAttribute("currentClassId", cid2);
				}else{
					logger.warn("该用户没有配置数据！");
					request.setAttribute("page", studentService.getListPage(page, -1));
					request.setAttribute("currentClassId", -1);
				}
			}
			request.getRequestDispatcher("class.jsp").forward(request,response);
		
		}else{
			//初始化查询
			if(list!=null && list.size()>0){
				Integer cid = list.get(0).getCid();
				request.setAttribute("currentClassId", cid);
				request.setAttribute("page", studentService.getListPage(1, cid));
			}
			request.getRequestDispatcher("class.jsp").forward(request,response);
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
